//设置统一的请求地址
function geturl(url) {
    // url = '/cgi-bin/' + url;
    url = 'http://192.168.1.222:80/cgi-bin/' + url;
    return url;
}

/**
 * 截取地址栏参数
 * @return {[type]} [description]
 */
function getArgs() {
    var args = [];
    var qs = location.search.length > 0 ? location.search.substring(1) : '';
    var items = qs.split('&');
    var item = null,
        name = null,
        value = null;
    for (var i = 0; i < items.length; i++) {
        item = items[i].split('=');
        name = item[0];
        value = item[1]
        args[name] = value;
    }
    return args;
}


/**
 * 获取当前状态下的hash值
 * @return {[type]} [description]
 */
function getHash() {
    var hash = location.hash.length > 0 ? location.hash.replace('#', '') : '';
    return hash;
}

/**
 * 时间戳转换为时间
 * tiem 为13位时间戳 
 * format 为显示时间的格式
 */
function format(time, format) {
    var dates = new Date(Number(time));
    var date = {
        "M+": dates.getMonth() + 1,
        "d+": dates.getDate(),
        "h+": dates.getHours(),
        "m+": dates.getMinutes(),
        "s+": dates.getSeconds(),
        "q+": Math.floor((dates.getMonth() + 3) / 3),
        "S+": dates.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (dates.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        };
    }
    return format;
}


/**
 * 操作成功提示
 * @param  {[type]} msg [description]
 * @param  {[type]} url [description]
 * @return {[type]}     [description]
 */
function success(msg, url) {
    window.swal({
        title: msg,
        type: 'success',
        allowOutsideClick: false,
        allowEscapeKey: false
    }, function() {
        if (url) {
            location.href = url;
        }
    });
}

/**
 * 操作失败提示
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
function failure(msg) {
    if (msg == 'token无效' || msg == '登录已过期') {
        msg = '登录已过期，请重新登录';
    }
    window.swal({
        title: msg,
        type: 'warning',
        allowOutsideClick: false,
        allowEscapeKey: false
    }, function() {
        if (msg == '登录已过期，请重新登录') {
            window.localStorage.removeItem('user_token');
            window.localStorage.removeItem('user_name');
            window.localStorage.removeItem('time_stamp');
            $("#userName").html('登录').attr('data-login', 'no');
            location.reload(true);
        } 
    });
}

/**
 * error提示
 * @param  {[type]} xhr [description]
 * @return {[type]}     [description]
 */
function error(xhr) {
    failure('请求出错了!!!');
}